<template>
  <div style="padding: 10px; width: 100%">
    <div style="margin: 10px 0">
      <el-input
        placeholder="请输入班级名称"
        v-model="search"
        style="width: 200px"
        @blur="blurUsername"
      >
        <template #append>
          <el-button icon="el-icon-search" @click="load"></el-button>
        </template>
      </el-input>

      <el-button type="primary" style="margin-left: 20px" @click="add"
        >添加</el-button
      >
    </div>
    <el-table :data="tableData" border stripe style="width: 60%" height="76vh">
      <el-table-column prop="id" label="ID" align="center"/>
      <el-table-column prop="clazzGrade" label="所属年级" align="center"/>
      <el-table-column prop="name" label="班级名称" align="center"/>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            @click="handleEdit(scope.row)"
            size="mini"
            type="primary"
            plain
            >编辑</el-button
          >
          <el-button
            @click="handleDelete(scope.row.id)"
            size="mini"
            type="danger"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加班级" v-model="dialogVisible" width="30%">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="班级编号" prop="id">
          <el-input v-model.number="form.id" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="班级名称" prop="name">
          <el-input v-model="form.name" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="所属年级" prop="gradeId">
          <el-select v-model="form.gradeId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="increase">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog title="修改班级信息" v-model="updateVisible" width="30%">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="班级编号" prop="id">
          <el-input
            v-model.number="form.id"
            style="width: 200px"
            disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="班级名称" prop="name">
          <el-input v-model="form.name" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="所属年级" prop="gradeId">
          <el-select v-model="form.gradeId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="updateVisible = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <div style="margin: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "../../utils/request";

export default {
  name: "Clazz",
  data() {
    return {
      rules: {
        id: [
          { required: true, message: "班级编号不能为空", trigger: "blur" },
          { type: "number", message: "班级编号必须为数值类型" },
        ],
        name: [
          { required: true, message: "班级编号不能为空", trigger: "blur" },
        ],
        gradeId: [{ required: true, message: "请选择年级", trigger: "blur" }],
      },
      tableData: [],
      form: {},
      options: [],
      search: "",
      dialogVisible: false,
      updateVisible: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
    };
  },
  created() {
    this.load();
    this.findGrade();
  },
  methods: {
    // 新增按钮事件
    add() {
      this.dialogVisible = true;
      this.form = {};
    },
    update() {
      request.put("/clazz/update", this.form).then((res) => {
        console.log(res);
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "更新成功",
          });
        } else {
          this.$message({
            type: "error",
            message: "更新失败",
          });
        }
        this.load();
        this.updateVisible = false;
      });
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.updateVisible = true;
    },
    // 查询年级信息
    findGrade() {
      request.get("/grade/all", {}).then((res) => {
        console.log(res);
        this.options = res.data;
        console.log("1111111");
        console.log(this.options[0].name);
      });
    },
    // 输入框失去焦点
    blurUsername() {
      this.load();
    },
    // 分页查询
    load() {
      request
        .get("/clazz/list", {
          params: {
            PageNum: this.currentPage,
            PageSize: this.pageSize,
            search: this.search,
          },
        })
        .then((res) => {
          console.log(res);
          console.log("111111111111");
          // console.log(res.data.records[0].gradeList[0].name);
          this.tableData = res.data.records;
          this.total = res.data.total;
          for (let i = 0; i <= res.data.records.length; i++) {
            this.tableData[i].clazzGrade = res.data.records[i].gradeList.name;
          }
        });
    },
    // 添加班级
    increase() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          request.post("/clazz/add", this.form).then((res) => {
            console.log(res);
            if (res.code === "0") {
              this.$message({
                type: "success",
                message: "添加成功",
              });
              this.dialogVisible = false;
              this.load();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        }
      });
    },
    // 删除班级信息
    handleDelete(id) {
      console.log(id);
      this.$confirm("是否删除该班级?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          request.delete("/clazz/delete/" + id).then((res) => {
            if (res.code === "0") {
              this.$message({
                type: "success",
                message: "删除成功",
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg(),
              });
            }
            this.load();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

     //改变当前每页的个数触发
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    //改变当前页码触发
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.load();
    },
  },
};
</script>

